package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.Category;
import com.itheima.domain.Dish;
import com.itheima.domain.Setmeal;
import com.itheima.dto.DishDto;
import com.itheima.service.CatgoryService;
import com.itheima.service.DishService;
import com.itheima.service.FlavorService;
import com.itheima.service.SetMealService;
import com.itheima.tool.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("dish")
public class DishController {

    @Autowired
    FlavorService flavorService;

    @Autowired
    DishService dishService;

    @Autowired
    CatgoryService cateService;

    @Autowired
    SetMealService setMealService;


    @GetMapping("/page")
    public R getDishByPage(Integer page, Integer pageSize, String name) {
        Page<Dish> pageinfo = new Page<>(page, pageSize);
        Page<DishDto> dtopage = new Page<>();


        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Dish::getUpdateTime);
        lqw.eq(StringUtils.isNotEmpty(name),Dish::getName,name);

        Page<Dish> page1 = dishService.page(pageinfo, lqw);

        BeanUtils.copyProperties(pageinfo,dtopage,"records");

        List<Dish> records = page1.getRecords();
        List<DishDto> collect = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();


            Category cateById = cateService.getById(categoryId);


            if (cateById != null) {
                String catName = cateById.getName();
                dishDto.setCategoryName(catName);
            }
            return dishDto;
        }).collect(Collectors.toList());

        dtopage.setRecords(collect);
        return R.success(dtopage);
    }

    @GetMapping("/{id}")

    public R getDishInfoById(@PathVariable Long id) {
        DishDto dishDto = dishService.checkById(id);
        return R.success(dishDto);
    }

    @PostMapping
    public R addDish(@RequestBody DishDto dto) {
        dishService.saveWithFavlor(dto);
        return R.success();
    }

    @PutMapping
    @Transactional
    public R updateDish(@RequestBody DishDto dishDto) {
        dishService.updateId(dishDto);
        return R.success();
    }

    @GetMapping("/list")
    public R getCategoryId(Dish dish){

        LambdaQueryWrapper<Dish> dlqw = new LambdaQueryWrapper<>();
        dlqw.eq(Dish::getCategoryId,dish.getCategoryId());
        dlqw.eq(Dish::getStatus,1);
        dlqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(dlqw);

        return R.success(list);
    }


}
